package com.nowcoder.community.service;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.util.RedisKeyUtil;
import com.nowcoder.community.util.XaCommunityConstant;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
@Service
public class FollowService implements XaCommunityConstant {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserService userService;
    public void follow(int userId,int entityType,int entityId){
        //关注的过程也必须是具有一致性
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                //ee是我关注的博主、
                //er是博主的粉丝
                String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType);
                String followerKey=RedisKeyUtil.getFollowerKey(entityType,entityId);
                //开启事务
                operations.multi();
                redisTemplate.opsForZSet().add(followeeKey,entityId,System.currentTimeMillis());
                redisTemplate.opsForZSet().add(followerKey,userId,System.currentTimeMillis());
                //提交事务
                return operations.exec();
            }
        });
    }
    //1.关注功能---将我的博主集合增加，将博主的粉丝集合增加
    public void unFollow(int userId,int entityType,int entityId){
        //关注的过程也必须是具有一致性
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                //ee是我关注的博主、
                //er是博主的粉丝
                String followeeKey = RedisKeyUtil.getFolloweeKey(userId,entityType);
                String followerKey = RedisKeyUtil.getFollowerKey(entityType,entityId);
                //开启事务
                operations.multi();
                redisTemplate.opsForZSet().remove(followeeKey,entityId);
                redisTemplate.opsForZSet().remove(followerKey,userId);
                //提交事务
                return operations.exec();
            }
        });
    }
    //2.取关功能---将我的博主集合减少，将博主的粉丝集合减少
    public long findFolloweeCount(int userId,int entityType){
        String followeeKey=RedisKeyUtil.getFolloweeKey(userId,entityType);
        return redisTemplate.opsForZSet().zCard(followeeKey);
    }
    // 查询关注的博主的数量
    public long findFollowerCount(int entityType,int entityId){
        String followerKey=RedisKeyUtil.getFollowerKey(entityType,entityId);
        return redisTemplate.opsForZSet().zCard(followerKey);
    }
    // 查询博主的粉丝数量
    public boolean hasFollowed(int userId, int entityType, int entityId) {
        String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
        return redisTemplate.opsForZSet().score(followeeKey, entityId) != null;
    }
    //查询当前用户是否有关注当前博主
    public List<Map<String,Object>>findFollowees(int userId,int offset,int limit){
        //创建key
        String followeeKey=RedisKeyUtil.getFolloweeKey(userId,ENTITY_TYPE_USER);
        //得到对应的set
        Set<Integer> targetIds=redisTemplate.opsForZSet().reverseRange(followeeKey,
                offset,offset+limit-1);
        
        //如果为空直接返回空
        if(targetIds==null)return null;
        //根据set的id值查询博主   根据score值换为设置日期
        List<Map<String,Object>>list=new ArrayList<>();
        for(Integer targetId:targetIds){
            Map<String ,Object>map=new HashMap<>();
            User user = userService.findUserById(targetId);
            Double score=redisTemplate.opsForZSet().score(followeeKey,targetId);
            map.put("user",user);
            map.put("followTime",new Date(score.longValue()));
            list.add(map);
        }
        //将结果放入结果map集中
        
        return list;
        
    }
    //查询当前用户关注的博主列表
    public List<Map<String, Object>>findFollowers(int userId, int offset, int limit){
        
        String followerKey=RedisKeyUtil.getFollowerKey(ENTITY_TYPE_USER,userId);
        Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followerKey,
                offset,offset+limit-1);
        
        List<Map<String, Object>> list = new ArrayList<>();
        
        for (Integer targetId : targetIds) {
            
            Map<String, Object> map = new HashMap<>();
            User user = userService.findUserById(targetId);
            Double score = redisTemplate.opsForZSet().score(followerKey, targetId);
            map.put("user", user);
            map.put("followTime", new Date(score.longValue()));
            list.add(map);
        }
        return list;
    }
    //查询该博主的粉丝列表
}